<template>
  <div class="app-container">
    <el-card class="tableset" shadow="never" :body-style="{ padding: '0px' }">
      <!-- 小标题 -->
      <div class="title">
        <span>打印机档案</span>
        <el-button
          type="primary"
          class="title-button-right"
          icon="el-icon-plus"
          style="margin-left: 0.5rem;"
          plain
          @click="openAddPrinterDialog"
        >新增打印机档案</el-button>
      </div>
      <!-- 小标题 -->

      <!-- 搜索关键词 -->
      <div class="funtion">
        <el-input
          v-model="get_printer_info_list.keyword"
          placeholder="请输入关键词"
          class="input-with-select"
          clearable
          @keyup.enter.native="getPrinterInfoList"
        >
          <el-select
            slot="prepend"
            v-model="get_printer_info_list.keywordType"
            placeholder="请选择"
            class="option"
          >
            <el-option label="品牌" value="0" />
            <el-option label="型号" value="1" />
            <el-option label="产品名称" value="2" />
            <el-option label="打印机类型" value="3" />
            <el-option label="打印机价格" value="4" />
          </el-select>
          <el-button slot="append" icon="el-icon-search" @click="getPrinterInfoList" />
        </el-input>
      </div>
      <!-- 搜索关键词 -->

      <!-- 数据表 -->
      <el-table
        v-loading="dataRequestLoading"
        element-loading-text="正在加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.5)"
        class="table"
        :data="data_printer_info_list.data"
        highlight-current-row
        stripe
        style="width: 100%;"
        :height="tableHeight"
        :max-height="tableHeight"
        @selection-change="getSelection"
        @sort-change="changeSort"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="brand" label="品牌" />
        <el-table-column prop="printer_path">
          <template slot-scope="{ row }">
            <el-image :src="getPath() + row.printer_path" style="width:60px;height:60px;" fit="cover" :preview-src-list="[getPath() + row.printer_path]" />
          </template>
        </el-table-column>
        <el-table-column prop="model_number" label="型号" />
        <el-table-column prop="produce_name" label="产品名称" />
        <el-table-column prop="printer_type" label="打印机类别" />
        <el-table-column prop="price" label="打印机价格" />
        <el-table-column prop="create_time" label="创建时间" sortable="custom" column-key="create_time" />
        <el-table-column fixed="right" label="操作" width="170px">
          <template slot-scope="{ row }">
            <el-button type="success" @click.stop="openCheckPrintDialog(row)">查看</el-button>
            <el-button type="primary" @click.stop="openEditPrintDialog(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据表 -->

      <!-- 数据表左下方功能区 -->
      <div>
        <el-button
          type="danger"
          class="card-bottom-button-left"
          icon="el-icon-delete"
          style="margin-left: 0.5rem;"
          plain
          :disabled="deleteDisabled"
          @click="deletePrintInfo"
        >删除</el-button>
      </div>
      <!-- 数据表左下方功能区 -->

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        background
        layout="total, sizes,prev, pager, next, jumper"
        :total="data_printer_info_list.total"
        :page-size="get_printer_info_list.pageSize"
        :page-sizes="[20, 50, 100]"
        :current-page="get_printer_info_list.currentPage"
        @size-change="changePageSize"
        @current-change="changeCurrentPage"
      />
      <!-- 分页 -->
    </el-card>

    <el-dialog
      title="查看打印机档案"
      :visible.sync="checkPrinterDialog"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form size="mini" label-width="140px">
        <el-divider content-position="left">产品信息</el-divider>
        <el-form-item label="品牌">
          <span>{{ change_printer_info.brand }}</span>
        </el-form-item>
        <el-form-item label="型号">
          <span>{{ change_printer_info.model_number }}</span>
        </el-form-item>
        <el-form-item label="产品名称">
          <span>{{ change_printer_info.produce_name }}</span>
        </el-form-item>
        <el-form-item label="打印机类别">
          <span>{{ change_printer_info.printer_type }}</span>
        </el-form-item>
        <el-form-item label="打印机价格">
          <span>{{ change_printer_info.price }}</span>
        </el-form-item>
        <el-form-item label="打印速度">
          <span>{{ change_printer_info.print_speed }}</span>
        </el-form-item>
        <div style="text-align: center;">
          <img :src="change_printer_info.printer_path" width="600" height="600">
        </div>
        <el-divider content-position="left">配置信息</el-divider>
        <el-form size="mini" :inline="true" label-width="140px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否有纸盒">
                <span>{{ show_option[change_printer_info.is_paper_box] }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纸盒或进纸器容量">
                <span>{{ change_printer_info.paper_box_capacity }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否支持彩色打印">
                <span>{{ show_option[change_printer_info.is_color] }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否支持双面打印">
                <span>{{ show_option[change_printer_info.is_two_sided_print] }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否支持邮箱打印">
                <span>{{ show_option[change_printer_info.is_email_print] }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否支持USB连接">
                <span>{{ show_option[change_printer_info.is_usb] }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否支持有线网络">
                <span>{{ show_option[change_printer_info.is_wired_network] }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否支持无线网络">
                <span>{{ show_option[change_printer_info.is_wireless_network] }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-divider content-position="left">墨水信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="黑色墨水品名及型号">
              <span>{{ change_printer_info.black_ink }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="黑色墨水打印量">
              <span>{{ change_printer_info.black_print_total }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="彩色墨水品名及型号">
              <span>{{ change_printer_info.color_ink }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="彩色墨水打印量">
              <span>{{ change_printer_info.color_print_total }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 新增/修改打印机档案dialog -->
    <el-dialog
      :title="changePrinterDialogTitle"
      :visible.sync="changePrinterDialog"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="changePrinterRulesForm"
        :model="change_printer_info"
        status-icon
        size="mini"
        label-width="140px"
      >
        <el-divider content-position="left">产品信息</el-divider>
        <el-form-item prop="brand" :rules="rules.input" label="品牌">
          <el-input v-model="change_printer_info.brand" placeholder="请输入品牌" clearable />
        </el-form-item>
        <el-form-item prop="model_number" :rules="rules.input" label="型号">
          <el-input v-model="change_printer_info.model_number" placeholder="请输入产品型号" clearable />
        </el-form-item>
        <el-form-item prop="printer_path" :rules="rules.image" label="打印机图片">
          <el-upload
            class="avatar-uploader"
            action
            :show-file-list="false"
            :auto-upload="false"
            :on-change="changeUploadImage"
          >
            <img v-if="change_printer_info.printer_path" :src="show_printer_path" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item prop="produce_name" :rules="rules.input" label="产品名称">
          <el-input v-model="change_printer_info.produce_name" placeholder="请输入产品名称" clearable />
        </el-form-item>
        <el-form-item prop="printer_type" :rules="rules.input" label="打印机类别">
          <el-input v-model="change_printer_info.printer_type" placeholder="请输入打印机类别" clearable />
        </el-form-item>
        <el-form-item prop="price" :rules="rules.input" label="打印机价格">
          <el-input v-model="change_printer_info.price" type="number" placeholder="请输入打印机价格" clearable />
        </el-form-item>
      </el-form>
      <el-form size="mini" :inline="true" label-width="140px">
        <el-form-item label="打印速度">
          <el-input v-model="change_printer_info.print_speed" placeholder="请输入打印速度" clearable />
        </el-form-item>
        <el-divider content-position="left">配置信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否有纸盒">
              <el-radio-group v-model="change_printer_info.is_paper_box" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸盒或进纸器容量">
              <el-input v-model="change_printer_info.paper_box_capacity" placeholder="请输入纸盒或进纸器容量" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否支持彩色打印">
              <el-radio-group v-model="change_printer_info.is_color" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否支持双面打印">
              <el-radio-group v-model="change_printer_info.is_two_sided_print" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否支持邮箱打印">
              <el-radio-group v-model="change_printer_info.is_email_print" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否支持USB连接">
              <el-radio-group v-model="change_printer_info.is_usb" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否支持有线网络">
              <el-radio-group v-model="change_printer_info.is_wired_network" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否支持无线网络">
              <el-radio-group v-model="change_printer_info.is_wireless_network" size="mini">
                <el-radio-button label="0">否</el-radio-button>
                <el-radio-button label="1">是</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="left">墨水信息</el-divider>
      <el-form size="mini" label-width="140px">
        <el-form-item label="黑色墨水品名及型号">
          <el-input v-model="change_printer_info.black_ink" placeholder="请输入黑色墨水品名及型号" clearable />
        </el-form-item>
        <el-form-item label="黑色墨水打印量">
          <el-input v-model="change_printer_info.black_print_total" placeholder="请输入黑色墨水打印量" clearable />
        </el-form-item>
        <el-form-item label="彩色墨水品名及型号">
          <el-input v-model="change_printer_info.color_ink" placeholder="请输入彩色墨水品名及型号" clearable />
        </el-form-item>
        <el-form-item label="彩色墨水打印量">
          <el-input v-model="change_printer_info.color_print_total" placeholder="请输入彩色墨水打印量" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="changePrinterDialog = false">取 消</el-button>
        <el-button type="primary" @click="changePrinterInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增/修改打印机档案dialog -->

    <!-- 剪切图片dialog -->
    <el-dialog title="图片剪裁" :visible.sync="UploadPrinterPathDialog" :close-on-click-modal="false">
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :info="option.info"
            :output-size="option.size"
            :output-type="option.outputType"
            :can-scale="option.canScale"
            :auto-crop="option.autoCrop"
            :auto-crop-width="option.autoCropWidth"
            :auto-crop-height="option.autoCropHeight"
            :fixed-box="option.fixedBox"
            :fixed="option.fixed"
            :fixed-number="option.fixedNumber"
            :full="option.full"
            :can-move-box="option.canMoveBox"
            :original="option.original"
            :center-box="option.centerBox"
            :info-true="option.infoTrue"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="UploadPrinterPathDialog = false">取 消</el-button>
        <el-button size="mini" type="primary" :loading="uploadImageLoading" @click="upLoadImage">确认</el-button>
      </div>
    </el-dialog>
    <!-- 剪切图片dialog -->
  </div>
</template>

<script>
import { getFormData } from '@/common/js/until'
import { uploadImage } from '@/api/upload'
import { getPrinterInfoList, changePrinterInfo, deletePrintInfo } from '@/api/printer'

export default {
  data() {
    return {
      // table高度自适应
      tableHeight: window.innerHeight - 265,

      // loading开关
      dataRequestLoading: false,

      // 按钮开关
      deleteDisabled: true,

      // loading开关
      uploadImageLoading: false,

      // 展示图片
      show_printer_path: '',

      // 图片裁剪框
      option: {
        img: '', // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 0.8, // 裁剪生成图片的质量
        outputType: 'png', // 裁剪生成图片的格式
        canScale: true, // 图片是否允许滚轮缩放
        autoCrop: true, // 是否默认生成截图框
        autoCropWidth: 700, // 默认生成截图框宽度
        autoCropHeight: 700, // 默认生成截图框高度
        fixedBox: true, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [1, 1], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: false, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: true, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },

      // dialog开关
      changePrinterDialog: false,
      UploadPrinterPathDialog: false,
      checkPrinterDialog: false,

      // 单选项显示
      show_option: {
        0: '否',
        1: '是'
      },

      // 发送请求参数
      get_printer_info_list: {
        pageSize: 20,
        currentPage: 1,
        keyword: '',
        keywordType: '0',
        sort_name: '',
        sort: 'desc'
      },

      // 返回值
      data_printer_info_list: {
        data: [],
        total: 0
      },

      // 新增/修改打印机档案dialog标题
      changePrinterDialogTitle: '',

      // 新增/修改打印机档案参数
      change_printer_info: {
        printer_id: '',
        brand: '',
        model_number: '',
        printer_path: '',
        produce_name: '',
        printer_type: '',
        price: '',
        is_paper_box: 0,
        paper_box_capacity: '',
        is_color: 0,
        is_two_sided_print: 0,
        is_email_print: 0,
        print_speed: '',
        is_usb: 1,
        is_wired_network: 0,
        is_wireless_network: 0,
        black_ink: '',
        black_print_total: '',
        color_ink: '',
        color_print_total: ''
      },

      // 获取选择项
      selection: [],

      // 表单校验规则
      rules: {
        input: { required: true, message: '请完成输入！', trigger: 'blur' },
        select: { required: true, message: '请完成选择！', trigger: 'blur' },
        image: { required: true, message: '请上传图片！', trigger: 'blur' }
      }
    }
  },
  mounted() {
    this.getPrinterInfoList() // 查询打印机档案信息列表
  },
  methods: {
    // 图片地址拼接
    getPath() {
      return process.env.VUE_APP_BASE_API
    },
    // 查询打印机档案信息列表
    getPrinterInfoList() {
      this.dataRequestLoading = true
      getPrinterInfoList(this.get_printer_info_list).then(res => {
        this.data_printer_info_list.data = res.result.showing
        this.data_printer_info_list.total = res.result.total
        this.dataRequestLoading = false
      })
    },

    // 新增/修改打印机档案
    changePrinterInfo() {
      this.$refs.changePrinterRulesForm.validate(valid => {
        if (valid) {
          changePrinterInfo(this.change_printer_info).then(res => {
            this.$notify({
              title: '成功',
              message: '保存成功！',
              type: 'success'
            })
            this.changePrinterDialog = false
          })
        } else {
          return false
        }
      })
    },

    // 获取选择项
    getSelection(selection) {
      if (selection.length === 0) {
        this.deleteDisabled = true
      } else {
        this.deleteDisabled = false
      }
      this.selection = selection
    },

    // 删除打印机档案信息（逻辑删除）
    deletePrintInfo() {
      this.$confirm('此操作将永久删除该打印机档案信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const printer_id = []
        this.selection.forEach(val => {
          printer_id.push(val.printer_id)
        })
        deletePrintInfo({ printer_id: printer_id }).then(res => {
          this.$notify({
            title: '成功',
            message: '删除打印机档案信息成功！',
            type: 'success'
          })
          this.getPrinterInfoList()
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' })
      })
    },

    // 打开新增打印机档案界面
    openAddPrinterDialog() {
      this.changePrinterDialogTitle = '新增打印机档案'
      this.change_printer_info = {
        printer_id: '',
        brand: '',
        model_number: '',
        printer_path: '',
        produce_name: '',
        printer_type: '',
        price: '',
        is_paper_box: 0,
        paper_box_capacity: '',
        is_color: 0,
        is_two_sided_print: 0,
        is_email_print: 0,
        print_speed: '',
        is_usb: 1,
        is_wired_network: 0,
        is_wireless_network: 0,
        black_ink: '',
        black_print_total: '',
        color_ink: '',
        color_print_total: ''
      }
      this.changePrinterDialog = true
    },

    // 打开修改打印机档案界面
    openEditPrintDialog(row) {
      this.changePrinterDialogTitle = '修改打印机档案'
      this.change_printer_info = {
        printer_id: row.printer_id,
        brand: row.brand,
        model_number: row.model_number,
        printer_path: row.printer_path,
        produce_name: row.produce_name,
        printer_type: row.printer_type,
        price: row.price,
        is_paper_box: row.is_paper_box,
        paper_box_capacity: row.paper_box_capacity,
        is_color: row.is_color,
        is_two_sided_print: row.is_two_sided_print,
        is_email_print: row.is_email_print,
        print_speed: row.print_speed,
        is_usb: row.is_usb,
        is_wired_network: row.is_wired_network,
        is_wireless_network: row.is_wireless_network,
        black_ink: row.black_ink,
        black_print_total: row.black_print_total,
        color_ink: row.color_ink,
        color_print_total: row.color_print_total
      }
      this.show_printer_path = process.env.VUE_APP_BASE_API + row.printer_path
      this.changePrinterDialog = true
    },

    // 打开查看打印机档案界面
    openCheckPrintDialog(row) {
      this.change_printer_info = {
        printer_id: row.printer_id,
        brand: row.brand,
        model_number: row.model_number,
        printer_path: process.env.VUE_APP_BASE_API + row.printer_path,
        produce_name: row.produce_name,
        printer_type: row.printer_type,
        price: row.price,
        is_paper_box: row.is_paper_box,
        paper_box_capacity: row.paper_box_capacity,
        is_color: row.is_color,
        is_two_sided_print: row.is_two_sided_print,
        is_email_print: row.is_email_print,
        print_speed: row.print_speed,
        is_usb: row.is_usb,
        is_wired_network: row.is_wired_network,
        is_wireless_network: row.is_wireless_network,
        black_ink: row.black_ink,
        black_print_total: row.black_print_total,
        color_ink: row.color_ink,
        color_print_total: row.color_print_total
      }
      this.checkPrinterDialog = true
    },

    // 切换每页条数方法
    changePageSize(data) {
      this.get_printer_info_list.pageSize = data
      this.getPrinterInfoList()
    },

    // 切换页码方法
    changeCurrentPage(data) {
      this.get_printer_info_list.currentPage = data
      this.getPrinterInfoList()
    },

    // 打开剪切界面或直接上传gif
    changeUploadImage(file, fileList) {
      const isLt5M = file.size / 1024 / 1024 < 2
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 2MB!')
        fileList.pop()
        return false
      }

      if (file.raw.type !== 'image/jpeg' && file.raw.type !== 'image/png') {
        this.$message.error('上传图片只支持jpeg和png!')
        fileList.pop()
        return false
      }
      // 将图片地址赋值给裁剪框显示图片
      this.$nextTick(() => {
        fileList.pop()
        this.option.img = URL.createObjectURL(file.raw)
        this.UploadPrinterPathDialog = true
      })
    },
    // 上传剪切后的图片
    upLoadImage() {
      this.$refs.cropper.getCropBlob(data => {
        const formData = getFormData(data, 'printer')
        uploadImage(formData).then(res => {
          this.change_printer_info.printer_path = res.result
          this.show_printer_path = process.env.VUE_APP_BASE_API + res.result
          this.UploadPrinterPathDialog = false
        })
      })
    },

    //  更换排序
    changeSort({ column, order }) {
      if (column.columnKey === 'create_time') {
        switch (order) {
          case 'descending':
            this.get_printer_info_list.sort_name = 'create_time'
            this.get_printer_info_list.sort = 'desc'
            this.getPrinterInfoList()
            break
          case 'ascending':
            this.get_printer_info_list.sort_name = 'create_time'
            this.get_printer_info_list.sort = 'asc'
            this.getPrinterInfoList()
            break
          default:
            this.get_printer_info_list.sort_name = ''
            this.get_printer_info_list.sort = 'desc'
            this.getPrinterInfoList()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
